package com.fare.taluo.application.impl.plane.channel;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.vo.auth.UserInfoVO;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.SourceTempVO;

@Transactional
@Scope("singleton")
@Service("sourceApplication")
public class SourceApplicationImpl implements SourceApplication {

	@Override
	public List<SourceEntityVO> findSourceEntityListByConditions(String name,
			String url, String webTyle,String sorttype, int pageNum, int pageSize) {
		SourceEntity source = new SourceEntity();
		List<SourceEntity> list = source.findSourceEntityListByConditions(name, url, webTyle,sorttype,pageNum, pageSize);
		
		List<SourceEntityVO> voList = new ArrayList<SourceEntityVO>();
		for (SourceEntity entity : list) {
			voList.add(this.domain2VO(entity));
		}
		return voList;
	}

	@Override
	public SourceEntityVO updateSourceEntity(Long id, Integer crawlStatus,UserInfoVO user) {
		SourceEntity source = new SourceEntity();
		source = source.updateSourceEntity(id, crawlStatus, user.getAccoutId());
		return this.domain2VO(source);
	}

	@Override
	public int getSourceEntityCount(String name, String url, String webTyle) {
		SourceEntity source = new SourceEntity();
		return source.getSourceEntityCount(name, url, webTyle);
	}

	@Override
	public List<SourceEntityVO> findAllSourceEntity() {
		SourceEntity source = new SourceEntity();
		List<SourceEntity> list = source.findAllSourceEntity();
		
		List<SourceEntityVO> voList = new ArrayList<SourceEntityVO>();
		for (SourceEntity entity : list) {
			SourceEntityVO vo  = new SourceEntityVO();
			vo.setCrawlStatus(entity.getCrawlStatus());
			vo.setId(entity.getId());
			vo.setName(entity.getName());
			vo.setUrl(entity.getUrl());
			vo.setUserId(entity.getUserId());
			vo.setCreateTime(entity.getUserCreateTime());
			vo.setWebTyle(entity.getWebTyle());
			vo.setHbaseKey(entity.getHbaseKey());
			vo.setCarrierKey(entity.getCarrierKey());
			vo.setShortName(entity.getShortName());
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public List<SourceEntityVO> findSourceEntityByName(String name) {
		// TODO Auto-generated method stub
		SourceEntity source = new SourceEntity();
		List<SourceEntity> list = source.findSourceEntityListByChanelName(name);
		List<SourceEntityVO> voList = new ArrayList<SourceEntityVO>();
		for (SourceEntity entity : list) {
			voList.add(this.domain2VO(entity));
		}
		return voList;
	}
	
	private SourceEntityVO domain2VO(SourceEntity entity){
		SourceEntityVO vo = new SourceEntityVO();
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCrawlStatus(entity.getCrawlStatus());
		vo.setCreateTime(entity.getUserCreateTime());
		vo.setHbaseKey(entity.getHbaseKey());
		vo.setId(entity.getId());
		vo.setName(entity.getName());
		vo.setShortName(entity.getShortName());
		vo.setUrl(entity.getUrl());
		vo.setUserId(entity.getUserId());
		vo.setWebTyle(entity.getWebTyle());
		return vo;
	}
	
	/**
	 * @see SourceApplicationImpl#findBycrawlStatus(int)
	 * */
	public List<SourceEntityVO> findBycrawlStatus(int crawlStatus){
		List<SourceEntityVO> sourceVoList = new ArrayList<SourceEntityVO>();
		List<SourceEntity> sourceList = SourceEntity.findByCrawlStatus(crawlStatus);
		for(SourceEntity source : sourceList){
			SourceEntityVO sourceVo = new SourceEntityVO();
			sourceVoList.add(this.domain2VO(source));
		}
		return sourceVoList;
	}
	
	public List<SourceEntityVO> findSourceEntityListByLine(String fromCity,
			String toCity){
		SourceEntity source = new SourceEntity();
		List<SourceEntityVO> sourceVoList = new ArrayList<SourceEntityVO>();
		List<SourceEntity> keyList = source.findSourceEntityListByLine(fromCity, toCity);
		for ( SourceEntity entity: keyList) {
			sourceVoList.add(this.domain2VO(entity));
		}
		return sourceVoList;
			
	}
	
	public List<SourceEntityVO> findSourceEntityListByCrawlLine(String fromCity,
			String toCity){
		SourceEntity source = new SourceEntity();
		List<SourceEntityVO> sourceVoList = new ArrayList<SourceEntityVO>();
		List<SourceEntity> keyList = source.findSourceEntityListByCrawlLine(fromCity, toCity);
		for (SourceEntity entity : keyList) {
			sourceVoList.add(this.domain2VO(entity));
		}
		return sourceVoList;
	}

	@Override
	public List<SourceTempVO> findSourceEntityListByCarrierKey(
			String carrierKey) {
		SourceEntity source = new SourceEntity();
		List<SourceEntity> list = source.findSourceEntityListByCarrierKey(carrierKey);
		List<SourceTempVO> voList = new ArrayList<SourceTempVO>();
		for (SourceEntity entity : list) {
			SourceTempVO vo  = new SourceTempVO();
			
			vo.setId(entity.getId());
			vo.setCompanyName(entity.getName());
			vo.setHbaseKey(entity.getHbaseKey());
			vo.setIdAndHbaseKeyAndName(entity.getId()+","+entity.getHbaseKey()+","+entity.getName());
			vo.setCarrierKey(entity.getCarrierKey());
			vo.setShortName(entity.getShortName());
			vo.setHbaseKey(entity.getHbaseKey());
			voList.add(vo);
		}
		return voList;
	}
	public SourceEntityVO findByHbaseKey(String hbaseKey){
		SourceEntity source = new SourceEntity().findByHbaseKey(hbaseKey);
		return this.domain2VO(source);
	}

	@Override
	public List<SourceEntityVO> findCrawlAirCompanys() throws Exception {
		List<SourceEntityVO> finalResult=new ArrayList<SourceEntityVO>();
		List<SourceEntity> es=new SourceEntity().findCrawlAirCompany();
		for(SourceEntity e:es){
			finalResult.add(this.domain2VO(e));
		}
		return finalResult;
	}

	@Override
	public List<SourceEntityVO> findCrawlChannel() throws Exception {
		List<SourceEntityVO> finalResult=new ArrayList<SourceEntityVO>();
		List<SourceEntity> es=SourceEntity.findCrawlChannels();
		for(SourceEntity e:es){
			finalResult.add(this.domain2VO(e));
		}
		return finalResult;
	}

	public List<SourceEntityVO> findByCarrierKey(String carrierKey){
		SourceEntity source = new SourceEntity();
		List<SourceEntity> list = source.findByCarrierKey(carrierKey);
		List<SourceEntityVO> voList = new ArrayList<SourceEntityVO>();
		for (SourceEntity entity : list) {
			voList.add(this.domain2VO(entity));
		}
		return voList;
	}

	@Override
	public SourceEntityVO find(Long id) {
		SourceEntity entity = SourceEntity.get(SourceEntity.class, id);
		return this.domain2VO(entity);
	}
	
}
